/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 8 Mar 2009 : Initial version created by gary
 */
package org.betonthemove.platform.test;

import org.betonthemove.platform.*;

public class Simulator {

	public static void main(String[] args) {
		Simulator app=new Simulator();

		app.run();
	}
	
	public void run() {
		try{
			BettingPlatform platform=
						BettingPlatformFactory.getBettingPlatform();
		
			platform.login(System.getProperty("username"),
							System.getProperty("password"));
			
			System.out.println("Logging in. Currency is: "+
						platform.getCurrency());
		
			menu(platform);
			        	
            platform.logout();
            
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
	}

	public void menu(BettingPlatform platform) throws Exception {
		boolean f_exit=false;
		
		while (f_exit == false) {
			System.out.println("=========================");
			System.out.println("1) Show Account Funds");
			System.out.println("2) Show Bets");
			System.out.println("3) Show Event Types");
			System.out.println("4) Show Markets");
			System.out.println("5) Show Market Entrants");
			System.out.println("6) Place Bet (Back)");
			System.out.println("7) Place Bet (Lay)");
			System.out.println("8) Cancel Bet");
			System.out.println("9) Exit");
			
			int option=getNumber("Selection option");
			
			switch(option) {
			case 1: showAccountFunds(platform);
					break;
			case 2: showBets(platform);
					break;
			case 3: showEventTypes(platform);
					break;
			case 4: showMarkets(platform);
					break;
			case 5: showMarketEntrants(platform);
					break;
			case 6: placeBet(platform, true);
					break;
			case 7: placeBet(platform, false);
					break;
			case 8: cancelBet(platform);
					break;
			case 9: f_exit = true;
					break;
			}
		}
	}

	public void showAccountFunds(BettingPlatform platform) throws Exception {
    	AccountFunds funds=platform.getAccountFunds();
    	
    	System.out.println("AVAILABLE FUNDS="+funds.getAvailableBalance());
    	System.out.println("FUNDS="+funds.getBalance());    	
	}
	
	public void showBets(BettingPlatform platform) throws Exception {
    	java.util.List<Bet> bets=platform.getBets();
    	
    	for (int i=0; i < bets.size(); i++) {
    		System.out.println(bets.get(i));
    	}
	}
	
	public void showEventTypes(BettingPlatform platform) throws Exception {
    	java.util.List<EventType> evtTypes=platform.getEventTypes();
    	
    	for (int i=0; i < evtTypes.size(); i++) {
    		System.out.println(evtTypes.get(i));
    	}    	
	}
	
	public void showMarkets(BettingPlatform platform) throws Exception {
    	int eventType=getNumber("Enter the event type id:");
    	
    	java.util.List<String> countries=new java.util.Vector<String>();
    	countries.add("GBR");
    	
    	int daysLookahead=3;
    	
    	MarketGroup group=platform.getMarkets(eventType,
    						daysLookahead, countries);
    	
    	int count=display(platform, group);
    	
    	System.out.println("Number of markets: "+count+" "+group.getMarketCount());
	}
	
	protected int display(BettingPlatform platform, MarketGroup group)
										throws Exception {
		int ret=0;
		java.util.List<MarketGroup> subGroups=platform.getSubGroups(group);
		
		for (int i=0; subGroups != null && i < subGroups.size(); i++) {
			ret += display(platform, subGroups.get(i));
		}
		
		java.util.List<Market> markets=platform.getMarkets(group);
		
		for (int i=0; markets != null && i < markets.size(); i++) {
			System.out.println(markets.get(i));
			ret++;
		}
		
		return(ret);
	}
	
	public void showMarketEntrants(BettingPlatform platform) throws Exception {
    	int marketId=getNumber("Enter the market id:");
    	
    	MarketDetails details=platform.getMarketDetails(marketId, false);
    	System.out.println("Bet delay="+details.getBetDelay());
    	
    	java.util.List<MarketEntrant> entrants=details.getEntrants();

    	for (int i=0; i < entrants.size(); i++) {
    		System.out.println(entrants.get(i));
    	}
	}
	
	public void placeBet(BettingPlatform platform, boolean back) throws Exception {
    	int marketId=getNumber("Enter the market id:");
    	int entrantId=getNumber("Enter the entrant id:");
		
    	double odds=getDouble("Enter the odds:");
    	double stake=getDouble("Enter the stake:");
    	
    	platform.placeBet(marketId, entrantId, back, odds, stake);
	}
	
	public void cancelBet(BettingPlatform  platform) throws Exception {
    	long betId=getLong("Enter the bet id:");
		
    	platform.cancelBet(betId);
	}
	
	public int getNumber(String mesg) {
		int ret=-1;
		byte[] b=new byte[1024];
		
		try {
			System.out.println(mesg);
			
			int len=System.in.read(b);
			
			String str=new String(b, 0, len).trim();
			
			ret = Integer.parseInt(str);
		
		} catch(Exception e) {
			e.printStackTrace();
		}

		return(ret);
	}
	
	public long getLong(String mesg) {
		long ret=-1;
		byte[] b=new byte[1024];
		
		try {
			System.out.println(mesg);
			
			int len=System.in.read(b);
			
			String str=new String(b, 0, len).trim();
			
			ret = Long.parseLong(str);
		
		} catch(Exception e) {
			e.printStackTrace();
		}

		return(ret);
	}
	
	public double getDouble(String mesg) {
		double ret=-1;
		byte[] b=new byte[1024];
		
		try {
			System.out.println(mesg);
			
			int len=System.in.read(b);
			
			String str=new String(b, 0, len).trim();
			
			ret = Double.parseDouble(str);
		
		} catch(Exception e) {
			e.printStackTrace();
		}

		return(ret);
	}	
}
